package com.copa.l404;

/**
 * @author copa
 * @createDate 2022-09-05 15:35
 * @function DFS
 */
public class Test2 {

    public int sumOfLeftLeaves(TreeNode root) {
        if (root.left == root.right && root.left == null) {
            return 0;
        }
        return sumLeft(root);
    }

    private int sumLeft(TreeNode root) {
        int sum = 0;
        if (root.left != null) {
            if (root.left.left == null && root.left.right == null) {
                sum += root.left.val;
            } else {
                sum += sumLeft(root.left);
            }
        }
        if (root.right != null) {
            sum += sumLeft(root.right);
        }
        return sum;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(new Test2().sumOfLeftLeaves(root));

    }
}
